package linkedlist;

/**
 * @author justnow
 * Created on 2020-10-02
 * Description
 *
 *
 */
public class Other2_AddTwoNumbers {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        ListNode current = dummy;
        int addOne = 0;
        boolean flag = false;
        while (l1 != null || l2 != null) {
            int val1 = 0;
            int val2 = 0;
            if (l1 != null) {
                val1 = l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                val2 = l2.val;
                l2 = l2.next;
            }


            int sum = val1 + val2 + addOne;
            if (sum > 9) {
                addOne = 1;
                sum = sum % 10;
                flag = true;
            } else {
                addOne = 0;
                flag = false;
            }
            current.next = new ListNode(sum);
            current = current.next;
        }

        if (flag) {
            current.next = new ListNode(1);
        }
        return dummy.next;
    }
}
